package com.example.trans.service.impl;

import com.example.trans.dao.AccountDao;
import com.example.trans.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * ClassName:IsolationService2
 * Package:com.example.trans.service.impl
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/5/11-21:41
 * @Version:v1.0
 */
@Service("i2")
public class IsolationService2 {

    @Resource(name = "accountDao")
    private AccountDao accountDao;


    //    @Transactional(readOnly = true,timeout = 10)
//    @Transactional(rollbackFor = RuntimeException.class)//只要发生RuntimeException或者这个异常的子类异常,都会回滚
    @Transactional(noRollbackFor = NullPointerException.class)//当异常是空指针异常的时候不回滚
    public void save(Account act) throws IOException {
//        //睡眠时间
//        try {
//            Thread.sleep(1000*20);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        accountDao.insert(act);
        //模拟异常
        if (1 == 1) {
//            throw new IOException();//不回滚
            throw new RuntimeException();
//            throw new NullPointerException();
        }
//        //睡眠时间
//        try {
//            Thread.sleep(1000*20);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
    }
}
